package com.elinkway.infinitemovies.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.elinkway.infinitemovies.utils.UIs;
import com.le123.ysdq.R;

/**
 * Created by zhaopeipei on 2016/4/21.
 */
public class LineView extends FrameLayout {

    private Paint mLinePaint;

    private String mUuid;

    public LineView(Context context, String uid) {
       this(context, null, 0);
    }

    public LineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
        initView();
    }

    private void initView() {
        LayoutInflater inflater =
                (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_line, this, true);
        mLinePaint = new Paint();
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.len_2dp));
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(getResources().getColor(R.color.black));
        mUuid = MoviesConstant.UUID.replace("-", "");
        LogUtils.e("lengye", "UUID " + mUuid);

    }

    protected void onDraw(Canvas canvas) {
        LogUtils.e("lengye", "onDraw");
        super.onDraw(canvas);
        for (int i = 0; i < mUuid.length(); i++) {
            int temp = Integer.parseInt(String.valueOf(mUuid.charAt(i)), 16);
            LogUtils.e("lengye", "onDraw " + temp);
            for (int j = 1; j <= 4; j++) {
                LogUtils.e("lengye", "onDraw &" + ((temp >> (4 - j)) & 1));
                if (((temp >> (4 - j)) & 1) == 1) {
                    LogUtils.e("lengye", "onDraw black paint");
                    mLinePaint.setColor(getResources().getColor(R.color.black_e0));
                } else {
                    LogUtils.e("lengye", "onDraw red paint");
                    mLinePaint.setColor(getResources().getColor(R.color.black));
                }
                canvas.drawLine((i * 4 + j - 1) * getResources().getDimensionPixelSize(R.dimen.len_2dp)
                        , 0, (i * 4 + j) * getResources().getDimensionPixelSize(R.dimen.len_2dp), 0, mLinePaint);
            }
        }
        mLinePaint.setColor(getResources().getColor(R.color.black));
        canvas.drawLine(128 * getResources().getDimensionPixelSize(R.dimen.len_2dp)
                , 0, UIs.getScreenWidth(), 0, mLinePaint);

    }

}
